<template>
  <a-layout class="layout-container">
    <!-- 侧边菜单 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      class="layout-sider"
    >
      <div class="logo">
        <h1 v-if="!collapsed">南通评估系统</h1>
        <h1 v-else>南</h1>
      </div>
      <a-menu
        mode="inline"
        theme="dark"
        :selectedKeys="selectedKeys"
        v-model:openKeys="openKeys"
        @select="handleMenuSelect"
      >
        <template v-for="menu in menus" :key="menu.path">
          <!-- 有子菜单的情况 -->
          <template v-if="menu.children && menu.children.length > 0">
            <a-sub-menu :key="menu.path">
              <template #title>
                <span>
                  <component :is="menu.icon" />
                  <span>{{ menu.name }}</span>
                </span>
              </template>
              <a-menu-item v-for="child in menu.children" :key="child.path">
                <component :is="child.icon" />
                <span>{{ child.name }}</span>
              </a-menu-item>
            </a-sub-menu>
          </template>
          
          <!-- 无子菜单的情况 -->
          <template v-else>
            <a-menu-item :key="menu.path">
              <component :is="menu.icon" />
              <span>{{ menu.name }}</span>
            </a-menu-item>
          </template>
        </template>
      </a-menu>
    </a-layout-sider>
    
    <a-layout>
      <!-- 顶部导航 -->
      <a-layout-header class="layout-header">
        <div class="header-content">
          <!-- 切换菜单按钮 -->
          <menu-unfold-outlined
            v-if="collapsed"
            class="trigger"
            @click="toggleCollapsed"
          />
          <menu-fold-outlined
            v-else
            class="trigger"
            @click="toggleCollapsed"
          />
          
          <!-- 右侧用户信息 -->
          <div class="header-right">
            <!-- 用户头像下拉菜单 -->
            <a-dropdown>
              <div class="avatar-wrapper">
                <a-avatar :src="getAvatarUrl(userInfo.avatar)" />
                <span class="username">{{ userInfo.nickname }}</span>
              </div>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile" @click="router.push('/profile')">
                    <user-outlined />
                    <span>个人中心</span>
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="handleLogout">
                    <logout-outlined />
                    <span>退出登录</span>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
        </div>
      </a-layout-header>
      
      <!-- 内容区域 -->
      <a-layout-content class="layout-content">
        <router-view />
      </a-layout-content>
      
      <!-- 页脚 -->
      <a-layout-footer class="layout-footer">
        南通评估系统 ©2024 南通评估团队
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  UserOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'
import { useMenu } from '@/composables/useMenu'
import { useAuthStore } from '@/stores/auth'
import { getUserInfo } from '@/utils/auth'
import { logout } from '@/utils/logout'

// 初始化
const router = useRouter()
const { menus, collapsed, selectedKeys, openKeys, toggleCollapsed, initMenus } = useMenu()
const authStore = useAuthStore()

// 用户信息（优先使用store中的信息，实时更新）
const userInfo = computed(() => {
  const storedUserInfo = getUserInfo()
  if (authStore.userInfo && Object.keys(authStore.userInfo).length > 0) {
    return {
      nickname: authStore.userInfo.nickname || '用户',
      avatar: authStore.userInfo.avatar || ''
    }
  }
  if (storedUserInfo) {
    return {
      nickname: storedUserInfo.nickname || '用户',
      avatar: storedUserInfo.avatar || ''
    }
  }
  return {
    nickname: '用户',
    avatar: ''
  }
})

// 获取完整的头像URL（通过nginx代理）
const getAvatarUrl = (url: string | undefined) => {
  if (!url) {
    return ''
  }
  // 如果已经是完整URL（包含http://或https://），直接返回
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url
  }
  // 如果是相对路径，拼接nginx地址
  if (url.startsWith('/')) {
    return 'http://127.0.0.1:80' + url
  }
  return url
}

// 初始化菜单
initMenus()

// 菜单选择事件
const handleMenuSelect = ({ key }: { key: string }) => {
  router.push(key)
}

// 退出登录
const handleLogout = () => {
  // 调用登出函数
  logout(true)
}
</script>

<style scoped>
.layout-container {
  min-height: 100vh;
}

.layout-sider {
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
}

.logo {
  height: 64px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.logo h1 {
  color: #fff;
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
}

.layout-header {
  padding: 0;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
}

.trigger {
  font-size: 20px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.avatar-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  color: rgba(0, 0, 0, 0.65);
}

.layout-content {
  margin: 24px;
  padding: 24px;
  background: #fff;
  border-radius: 8px;
  min-height: 280px;
}

.layout-footer {
  text-align: center;
  padding: 16px 50px;
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
}
</style>
